package com.zoweunion.mechanic.controller.api;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.app.TSSsInsuranceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.entity.TSSsInsuranceEntity;


import com.zoweunion.mechanic.model.ResponseBean;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/insurance")
public class TSSsInsuranceController extends BaseController {
    @Autowired
    private TSSsInsuranceService tSSsInsuranceService;

    /**
     * 列表
     */
    @GetMapping("/insuranceList")
    public ResponseBean tSSsInsuranceList(@RequestParam Map<String, Object> params, HttpServletRequest request)  throws MyException{
        User currentUser = getCurrentUser(request);
        PageInfo info = tSSsInsuranceService.queryPage(params,currentUser);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 新增
     */
    @PostMapping("/insertOrUpdate")
    public ResponseBean tSSsInsuranceInsertUpdate(@RequestBody TSSsInsuranceEntity params, HttpServletRequest request) throws MyException {
        User currentUser = getCurrentUser(request);
        Long id = null;
        if (params.getId()!=null){
            id = tSSsInsuranceService.updateInsurance(params,currentUser);
        }else {
            id = tSSsInsuranceService.saveInsurance(params,currentUser);
        }
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", id);
    }

    /**
     * 详情
     * @param params
     * @param request
     * @return
     * @throws MyException
     */
    @GetMapping("/insuranceDetail")
    public ResponseBean insuranceDetail(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        User currentUser = getCurrentUser(request);
        String id = params.get("id").toString();
        TSSsInsuranceEntity result = tSSsInsuranceService.getInsuranceDetail(id);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 修改保险状态
     * @param params
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("/updateStatus")
    public ResponseBean updateStatus(@RequestBody TSSsInsuranceEntity params, HttpServletRequest request) throws MyException {
        User currentUser = getCurrentUser(request);
        tSSsInsuranceService.updateStatus(params,currentUser);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    @PostMapping("/updateCertificateExpress")
    public ResponseBean updateCertificateExpress(@RequestBody TSSsInsuranceEntity params,
                                                 HttpServletRequest request) throws MyException {

        User currentUser = getCurrentUser(request);
        return tSSsInsuranceService.updateExpress(params,currentUser);
    }

    @GetMapping("/getPayStatus")
    public ResponseBean getPayStatus(@RequestParam Map<String, Object> params,
                                     HttpServletRequest request) throws MyException {
        logBefore(logger,"获取支付状态");
        User currentUser = getCurrentUser(request);
        Map<String,Object> result = tSSsInsuranceService.getPayStatus(params,currentUser);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }
    @GetMapping("/getPdf")
    public void showPdf1(HttpServletRequest request, HttpServletResponse response) {

        response.setContentType("application/pdf");
        response.setHeader("Access-Control-Allow-Origin", "*"); // 解决请求头跨域问题
        String path = request.getParameter("path");
        ServletOutputStream sos = null;
        BufferedInputStream bis = null;
        try {
            sos = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //这里的 destUrl目前是固定的,实际应用中需要动态获取
        String destUrl = path; // 远程文件途径，这是我放在远程服务器的
        //tomcat /webapp下的文件，该链接可通过浏览器直接打开，你可以用它来进行测试；
        URL url = null;
        try {
            url = new URL(destUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection httpUrl = null;
        try {
            httpUrl = (HttpURLConnection) url.openConnection();
            // 连接指定的网络资源
            httpUrl.connect();
            // 获取网络输入流
            bis = new BufferedInputStream(httpUrl.getInputStream());
            int b;
            while ((b = bis.read()) != -1) {
                sos.write(b);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                sos.close(); // 这里有点和c语言里的读取文件有点像
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }
    /**
     * 查询物流信息
     */

//    @GetMapping("/logisticsExpress")
//    public ResponseBean logisticsExpress(@RequestParam Map<String, Object> map,
//                                         HttpServletRequest request) throws MyException {
//        TSSsInsuranceEntity order = tSSsInsuranceService.logisticsExpress(map);
//        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", order);
//    }






}
